package com.zilla.android.zillacore.libzilla.api;

import com.zilla.android.zillacore.libzilla.file.AddressManager;
import com.zilla.android.zillacore.libzilla.file.PropertiesManager;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;

/**
 * Created by zilla on 14-7-17.
 */
public class ZillaApi {
    /**
     * 基础的RestAdapter
     */
    public static RestAdapter SimpleRestAdapter = new RestAdapter.Builder()
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setEndpoint(AddressManager.getHost())
            .build();

    public static RestAdapter NormalRestAdapter = new RestAdapter.Builder()
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setEndpoint(AddressManager.getHost())
            .build();

    public static RestAdapter loginRestAdapter = new RestAdapter.Builder()
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setEndpoint(AddressManager.getLoginHost())
            .build();
    /**
     * 返回自定义的RestAdpater
     *
     * @param url
     * @param requestInterceptor
     * @return
     */
    public static RestAdapter getCustomRESTAdapter(String url, RequestInterceptor requestInterceptor) {
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(url)
                .setRequestInterceptor(requestInterceptor)
                .build();
        return setLog(restAdapter);
    }

    /**
     * 返回默认的RESTAdapter
     *
     * @param requestInterceptor
     * @return
     */
    public static RestAdapter getRESTAdapter(RequestInterceptor requestInterceptor) {
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(AddressManager.getHost())
                .setRequestInterceptor(requestInterceptor)
                .build();
        return setLog(restAdapter);
    }

    private static RestAdapter setLog(RestAdapter restAdapter) {
        if (Boolean.parseBoolean(PropertiesManager.get("log"))) {
            restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);
        } else {
            restAdapter.setLogLevel(RestAdapter.LogLevel.NONE);
        }
        return restAdapter;
    }
}
